﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Chromos
{
	public class RectangleF
	{
		private float iX, iY, iWidth, iHeight;
		private static int t = 1;
		public RectangleF(float X, float Y, float Width, float Height)
		{
			this.iX = X * t;
			this.iY = Y * t;
			this.iWidth = Width * t;
			this.iHeight = Height * t;
		}

		/// <summary>
		/// Modifies outR
		/// </summary>
		/// <param name="rect1"></param>
		/// <param name="rect2"></param>
		/// <param name="outR"></param>
		public static void Intersect(RectangleF rect1, RectangleF rect2, RectangleF outR)
		{
			// Set corner
			if (rect1.X <= rect2.X && rect1.Y <= rect2.Y)
			{
				//		rect2
				// rect1
				outR.X = rect2.X;
				outR.Y = rect2.Y;
			}
			else if (rect1.X <= rect2.X)
			{
				// rect1
				//		rect2
				outR.X = rect2.X;
				outR.Y = rect1.Y;
			}
			else if (rect1.Y > rect2.Y)
			{
				//		rect1
				// rect2
				outR.X = rect1.X;
				outR.Y = rect1.Y;
			}
			else
			{
				// rect2
				//		rect1
				outR.X = rect1.X;
				outR.Y = rect2.Y;
			}

			// Set width and height (using upper-right corners)
			float x1, y1, x2, y2;
			x1 = rect1.X + rect1.Width;
			y1 = rect1.Y + rect1.Height;
			x2 = rect2.X + rect2.Width;
			y2 = rect2.Y + rect2.Height;
			if (x1 <= x2 && y1 <= y2)
			{
				//		2
				// 1
				outR.Width = x1 - outR.X;
				outR.Height = y1 - outR.Y;
			}
			else if (x1 <= x2)
			{
				// 1
				//		2
				outR.Width = x1 - outR.X;
				outR.Height = y2 - outR.Y;
			}
			else if (y1 > y2)
			{
				//		1
				// 2
				outR.Width = x2 - outR.X;
				outR.Height = y2 - outR.Y;
			}
			else
			{
				// 2
				//		1
				outR.Width = x2 - outR.X;
				outR.Height = y1 - outR.Y;
			}

			if (outR.Width < 0)
				outR.Width = 0;
			if (outR.Height < 0)
				outR.Height = 0;
		}

		RectangleF intRect;
		public bool Intersects(RectangleF rect)
		{
			if (intRect == null)
				intRect = new RectangleF(0, 0, 0, 0);
			Intersect(this, rect, intRect);
			if (intRect.Width != 0 && intRect.Height != 0)
				return true;
			return false;
		}

		public void Inflate(float x, float y)
		{
			this.iX -= (x * t);
			this.iY -= (y * t);
			this.iWidth += 2*(x * t);
			this.iHeight += 2*(y * t);
		}

		/// <summary>
		/// Converts this back to a rectangle by dividing each component by the internal scale factor
		/// </summary>
		/// <returns></returns>
		public Rectangle toRectangle()
		{
			return new Rectangle((int)this.X, (int)this.Y, (int)this.Width, (int)this.Height);
		}
		public Rectangle toUnscaledRectangle()
		{
			return new Rectangle((int)this.iX, (int)this.iY, (int)this.iWidth, (int)this.iHeight);
		}

		public float X
		{
			get
			{
				return iX / t;
			}
			set
			{
				iX = value * t;
			}
		}
		public float Y
		{
			get
			{
				return iY / t;
			}
			set
			{
				iY = value * t;
			}
		}
		public float Width
		{
			get
			{
				return iWidth / t;
			}
			set
			{
				iWidth = value * t;
			}
		}
		public float Height
		{
			get
			{
				return iHeight / t;
			}
			set
			{
				iHeight = value * t;
			}
		}
	}
}
